package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.lang.model.type.ExecutableType;
import java.time.LocalTime;

@Component
public class SpringListener {


    /**  这个是直接中队列中获取信息消费
     *
     * @param msg  队列中的信息
     * @throws InterruptedException
     */
    @RabbitListener(queues = "simple.queue")//队列名称
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息：【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }


    /** 广播,FanoutExchange
     * 同一个交换机绑定俩个队列
     * @param msg
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenerFanout(String msg){
        System.out.println("消费者一接收到的信息："+msg);
    }


    @RabbitListener(queues = "fanout.queue2")
    public void listenerFanout2(String msg){
        System.out.println("消费者二接收到的信息："+msg);
    }


    /*
    springamqp，directexchage，交换机按照规则路由给每个队列
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),//队列名
            exchange = @Exchange(name = "lin.direct",type = ExchangeTypes.DIRECT),
            key = {"green","red"}
    ))
    public void listenerDriect1(String msg){
        System.out.println("队列一中有的key是(green,red),接收到的信息："+msg);
    }



    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("direct.queue2"),
            exchange = @Exchange(name = "lin.direct",type = ExchangeTypes.DIRECT),
            key = {"red","bule"}//"{red,bule}"

    ))
    public void listenerDirect2(String msg){
        System.out.println("队列二中的key有red，bule，接收到的信息是"+msg);
    }




}
